<template>
  <div class="led-activities">
    <div class="page-header">
      <h2>我负责的活动</h2>
      <el-button type="primary" @click="dialogVisible = true">新增活动</el-button>
    </div>

    <!-- 添加查询表单 -->
    <el-form :inline="true" class="search-form">
      <el-form-item label="活动标题">
        <el-input v-model="searchForm.title" placeholder="请输入活动标题" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态">
          <el-option label="全部" value="" />
          <el-option label="进行中" value="进行中" />
          <el-option label="待定" value="待定" />
          <el-option label="已完成" value="已完成" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="searchForm.timeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="filteredActivities">
      <el-table-column prop="activityId" label="活动编号" width="100" />
      <el-table-column prop="title" label="活动标题" />
      <el-table-column prop="maxParticipants" label="参加人数" width="100" />
      <el-table-column prop="startTime" label="开始时间" width="180" />
      <el-table-column prop="endTime" label="结束时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="scope">
          <el-button 
            type="primary" 
            @click="handleManageAttendance(scope.row)"
          >
            签到管理
          </el-button>
          <el-button 
            type="success" 
            @click="handleAssessment(scope.row)"
          >
            考核评分
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="新增活动">
      <el-form :model="activityForm" label-width="100px">
        <el-form-item label="活动标题">
          <el-input v-model="activityForm.title" />
        </el-form-item>
        <el-form-item label="活动内容">
          <el-input type="textarea" v-model="activityForm.content" />
        </el-form-item>
        <el-form-item label="参加人数">
          <el-input-number v-model="activityForm.maxParticipants" :min="1" />
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
            v-model="activityForm.activityTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="报名时间">
          <el-date-picker
            v-model="activityForm.signupTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getActivities, createActivity, updateActivity } from '@/api/activity'

export default {
  data() {
    return {
      activities: [],
      dialogVisible: false,
      activityForm: {
        title: '',
        content: '',
        maxParticipants: 1,
        activityTime: [],
        signupTime: []
      },
      searchForm: {
        title: '',
        status: '',
        timeRange: []
      }
    }
  },
  created() {
    this.fetchActivities()
  },
  computed: {
    filteredActivities() {
      return this.activities.filter(activity => {
        const matchTitle = !this.searchForm.title || 
          activity.title.includes(this.searchForm.title)
        const matchStatus = !this.searchForm.status || 
          activity.status === this.searchForm.status
        let matchTime = true
        if (this.searchForm.timeRange && this.searchForm.timeRange.length === 2) {
          const startDate = this.searchForm.timeRange[0]
          const endDate = this.searchForm.timeRange[1]
          const activityDate = new Date(activity.startTime)
          matchTime = activityDate >= startDate && activityDate <= endDate
        }
        return matchTitle && matchStatus && matchTime
      })
    }
  },
  methods: {
    getStatusType(status) {
      const types = {
        '进行中': 'success',
        '待定': 'warning',
        '已完成': 'info'
      }
      return types[status] || 'info'
    },
    handleSearch() {
      // 查询逻辑已通过计算属性实现
    },
    resetSearch() {
      this.searchForm = {
        title: '',
        status: '',
        timeRange: []
      }
    },
    handleManageAttendance(activity) {
      this.$router.push(`/volunteer/attendance/${activity.activityId}`)
    },
    handleAssessment(activity) {
      this.$router.push(`/volunteer/assessment/${activity.activityId}`)
    },
    async fetchActivities() {
      try {
        const data = await getActivities(this.searchForm)
        this.activities = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleSubmit() {
      try {
        if (this.activityForm.id) {
          await updateActivity(this.activityForm.id, this.activityForm)
        } else {
          await createActivity(this.activityForm)
        }
        this.$message.success('保存成功')
        this.dialogVisible = false
        this.fetchActivities()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}
</style> 